package com.mike.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mike.common.ServerResponse;
import com.mike.dao.CategoryMapper;
import com.mike.pojo.Category;
import com.mike.service.ICategoryService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * Created by HP on 2018/3/3.
 */
@Service("categoryService")
public class CategoryServiceImpl implements ICategoryService{

    private Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ServerResponse addcategory(String categoryName, Integer parentId) {
        if(parentId == null || StringUtils.isBlank(categoryName)){
            return ServerResponse.createErrorMessageResponse("添加品类参数错误");
        }
        Category category =new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        category.setStatus(true);//分类可用
        int rowCount = categoryMapper.insert(category);

        if(rowCount > 0){
    return ServerResponse.createSuccessResponse("添加品类成功");
        }
        return ServerResponse.createErrorMessageResponse("添加品类失败");
    }


    @Override
    public ServerResponse updateCategoryName(Integer categoryId, String categoryName) {
        if(categoryId == null || StringUtils.isBlank(categoryName)){
            return ServerResponse.createErrorMessageResponse("更新品类参数错误");
        }
        Category category = new Category();
        category.setId(categoryId);
        category.setName(categoryName);

        int rowCount = categoryMapper.updateByPrimaryKeySelective(category);
        if(rowCount > 0){
            return ServerResponse.createErrorMessageResponse("更新品类参数错误");
        }
            return ServerResponse.createErrorMessageResponse("更新品类名字失败");
    }

    @Override
    public ServerResponse<List<Category>> getChildrenParallelCategory(Integer categoryId) {
        List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId);
        if(CollectionUtils.isEmpty(categoryList)){
            logger.info("未找到当前分类的子分类");
        }
        return ServerResponse.createSuccessResponse(categoryList);
    }


    /**
     * 查询本节点的id及孩子节点的id
     * @param categoryId
     * @return
     */
    @Override
    public ServerResponse<List<Integer>> selectCategoryAndChildrenById(Integer categoryId) {
        //1-使用了GUAVA缓存工具的Sets集合对象com.google.common.collect.Sets
        Set<Category> categorySet = Sets.newHashSet();
        //思考问题:Set中的Category为自定义的复杂对象（多个属性），要根据怎样的条件排重
        //2-调用递归方法findChildCategory,查看器定义，返现其中的参数CategorySet既是参数也是返回值
        findChildCategory(categorySet,categoryId);
        //3-使用了GUAVA缓存工具的Lists集合对象com.google.common.collect.Lists
        List<Integer> categoryIdList = Lists.newArrayList();
        //4-将Set中的值遍历赋值给List
        if(categoryId != null){
            for(Category categoryItem : categorySet){
                categoryIdList.add(categoryItem.getId());
            }
        }
        //5-返回date=categoryIdList的ServerResponse对象
        return ServerResponse.createSuccessResponse(categoryIdList);
    }

    //递归算法的应用
    //注意：参数Set<Category> categorySet与返回值Set<Category>在递归中实为同一个集合对象，用来存储数据
    private Set<Category> findChildCategory(Set<Category> categorySet, Integer categoryId){
        //1-获得品类节点本身，如果不为空，就放入到Set集合中
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        if(category != null){
            categorySet.add(category);
        }
        //2-调用dao层方法，获取子节点，Mybatis不返回空集合对象
        List<Category> categoryList =categoryMapper.selectCategoryChildrenByParentId(categoryId);
        //3-遍历品类集合
        for(Category categoryItem : categoryList){
            //4-递归调用findChildCategory方法
            findChildCategory(categorySet,categoryItem.getId());
        }
        return categorySet;
    }

}
